<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" @submit.native.prevent label-width="68px">
      <el-form-item label="股票代码" prop="symbol">
        <el-input
          v-model="queryParams.symbol"
          placeholder="请输入股票代码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      <el-form-item label="股票名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入股票名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['stock:current:export']"
        >导出</el-button>
      </el-form-item>
    </el-form>

    <el-table size="medium" v-loading="loading" :data="currentList" @selection-change="handleSelectionChange">
      <el-table-column label="股票代码" align="center" prop="symbol" fixed="left">
        <template slot-scope="scope">
          <el-link @click="redictDetail(scope.row.symbol)" :underline="false" target="_blank">
            {{ scope.row.symbol }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column label="股票名称" align="center" prop="name" fixed="left"/>
      <el-table-column label="最新价" align="center" prop="now">
        <template slot-scope="scope">
          <strong>
          <font v-if="scope.row.change > 0" color="red">{{ scope.row.now }}</font>
          <font v-else color="green">{{ scope.row.change }}</font>
          </strong>
        </template>
      </el-table-column>
      <el-table-column label="涨跌额" align="center" prop="change">
        <template slot-scope="scope">
          <font v-if="scope.row.change > 0" color="red">{{ scope.row.change }}</font>
          <font v-else color="green">{{ scope.row.change }}</font>
        </template>
      </el-table-column>
      <el-table-column label="涨跌幅" align="center" prop="pctChg">
        <template slot-scope="scope">
          <font v-if="scope.row.pctChg > 0" color="red">{{ scope.row.pctChg }}%</font>
          <font v-else color="green">{{ scope.row.pctChg }}%</font>
        </template>
      </el-table-column>
      <el-table-column label="开盘价" align="center" prop="open">
        <template slot-scope="scope">
          <font v-if="scope.row.open < scope.row.preClose" color="green">{{ scope.row.open }}</font>
          <font v-else color="red">{{ scope.row.open }}</font>
        </template>
      </el-table-column>
      <el-table-column label="最高价" align="center" prop="high">
        <template slot-scope="scope">
          <font v-if="scope.row.high < scope.row.preClose" color="green">{{ scope.row.high }}</font>
          <font v-else color="red">{{ scope.row.high }}</font>
        </template>
      </el-table-column>
      <el-table-column label="最低价" align="center" prop="low">
        <template slot-scope="scope">
          <font v-if="scope.row.low < scope.row.preClose" color="green">{{ scope.row.low }}</font>
          <font v-else color="red">{{ scope.row.low }}</font>
        </template>
      </el-table-column>
      <el-table-column label="昨收价" align="center" prop="preClose" />
      <el-table-column label="振幅" align="center" prop="amplitude" />
      <el-table-column label="换手率" align="center" prop="turnoverrate" />
      <el-table-column label="成交量" align="center" prop="vol" />
      <el-table-column label="成交额" align="center" prop="amount">
        <template slot-scope="scope">
          {{ parseFloat(scope.row.amount/100000000).toFixed(2) }}亿
        </template>
      </el-table-column>
      <el-table-column label="市盈率" align="center" prop="peRatio" />
      <el-table-column label="市盈率(动)" align="center" prop="peDynamicRatio" />
      <el-table-column label="5分钟涨跌" align="center" prop="fiveMinuteChange">
        <template slot-scope="scope">
          <font v-if="scope.row.fiveMinuteChange > 0" color="red">{{ scope.row.fiveMinuteChange }}%</font>
          <font v-else color="green">{{ scope.row.fiveMinuteChange }}%</font>
        </template>
      </el-table-column>
      <el-table-column label="60日涨跌" align="center" prop="sixtyDayChange">
        <template slot-scope="scope">
          <font v-if="scope.row.sixtyDayChange > 0" color="red">{{ scope.row.sixtyDayChange }}%</font>
          <font v-else color="green">{{ scope.row.sixtyDayChange }}%</font>
        </template>
      </el-table-column>
      <el-table-column label="年度涨跌" align="center" prop="yearChange">
        <template slot-scope="scope">
          <font v-if="scope.row.yearChange > 0" color="red">{{ scope.row.yearChange }}%</font>
          <font v-else color="green">{{ scope.row.yearChange }}%</font>
        </template>
      </el-table-column>
      <el-table-column label="流通市值" align="center" prop="freeMarketCap">
        <template slot-scope="scope">
          {{ parseFloat(scope.row.freeMarketCap/100000000).toFixed(2) }}亿
        </template>
      </el-table-column>
      <el-table-column label="总市值" align="center" prop="totalMarketCap">
        <template slot-scope="scope">
          {{ parseFloat(scope.row.totalMarketCap/100000000).toFixed(2) }}亿
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改股票当前信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="股票代码" prop="symbol">
          <el-input v-model="form.symbol" placeholder="请输入股票代码" />
        </el-form-item>
        <el-form-item label="股票名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入股票名称" />
        </el-form-item>
        <el-form-item label="最新价" prop="now">
          <el-input v-model="form.now" placeholder="请输入最新价" />
        </el-form-item>
        <el-form-item label="收盘价" prop="close">
          <el-input v-model="form.close" placeholder="请输入收盘价" />
        </el-form-item>
        <el-form-item label="涨跌额" prop="change">
          <el-input v-model="form.change" placeholder="请输入涨跌额" />
        </el-form-item>
        <el-form-item label="涨跌幅" prop="pctChg">
          <el-input v-model="form.pctChg" placeholder="请输入涨跌幅" />
        </el-form-item>
        <el-form-item label="开盘价" prop="open">
          <el-input v-model="form.open" placeholder="请输入开盘价" />
        </el-form-item>
        <el-form-item label="最高价" prop="high">
          <el-input v-model="form.high" placeholder="请输入最高价" />
        </el-form-item>
        <el-form-item label="最低价" prop="low">
          <el-input v-model="form.low" placeholder="请输入最低价" />
        </el-form-item>
        <el-form-item label="昨收价" prop="preClose">
          <el-input v-model="form.preClose" placeholder="请输入昨收价" />
        </el-form-item>
        <el-form-item label="振幅" prop="amplitude">
          <el-input v-model="form.amplitude" placeholder="请输入振幅" />
        </el-form-item>
        <el-form-item label="换手率" prop="turnoverrate">
          <el-input v-model="form.turnoverrate" placeholder="请输入换手率" />
        </el-form-item>
        <el-form-item label="成交量" prop="vol">
          <el-input v-model="form.vol" placeholder="请输入成交量" />
        </el-form-item>
        <el-form-item label="成交额" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入成交额" />
        </el-form-item>
        <el-form-item label="市盈率" prop="peRatio">
          <el-input v-model="form.peRatio" placeholder="请输入市盈率" />
        </el-form-item>
        <el-form-item label="市盈率" prop="peDynamicRatio">
          <el-input v-model="form.peDynamicRatio" placeholder="请输入市盈率" />
        </el-form-item>
        <el-form-item label="5分钟涨跌幅" prop="fiveMinuteChange">
          <el-input v-model="form.fiveMinuteChange" placeholder="请输入5分钟涨跌幅" />
        </el-form-item>
        <el-form-item label="60日涨跌幅" prop="sixtyDayChange">
          <el-input v-model="form.sixtyDayChange" placeholder="请输入60日涨跌幅" />
        </el-form-item>
        <el-form-item label="年度涨跌幅" prop="yearChange">
          <el-input v-model="form.yearChange" placeholder="请输入年度涨跌幅" />
        </el-form-item>
        <el-form-item label="总市值" prop="totalMarketCap">
          <el-input v-model="form.totalMarketCap" placeholder="请输入总市值" />
        </el-form-item>
        <el-form-item label="流通市值" prop="freeMarketCap">
          <el-input v-model="form.freeMarketCap" placeholder="请输入流通市值" />
        </el-form-item>
        <el-form-item label="交易日期" prop="tradeDate">
          <el-input v-model="form.tradeDate" placeholder="请输入交易日期" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCurrent, getCurrent, delCurrent, addCurrent, updateCurrent, exportCurrent } from "@/api/stock/current";

export default {
  name: "Current",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 股票当前信息表格数据
      currentList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        symbol: null,
        name: null,
        now: null,
        close: null,
        change: null,
        pctChg: null,
        open: null,
        high: null,
        low: null,
        preClose: null,
        amplitude: null,
        turnoverrate: null,
        vol: null,
        amount: null,
        peRatio: null,
        peDynamicRatio: null,
        fiveMinuteChange: null,
        sixtyDayChange: null,
        yearChange: null,
        totalMarketCap: null,
        freeMarketCap: null,
        tradeDate: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询股票当前信息列表 */
    getList() {
      this.loading = true;
      listCurrent(this.queryParams).then(response => {
        this.currentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        symbol: null,
        name: null,
        now: null,
        close: null,
        change: null,
        pctChg: null,
        open: null,
        high: null,
        low: null,
        preClose: null,
        amplitude: null,
        turnoverrate: null,
        vol: null,
        amount: null,
        peRatio: null,
        peDynamicRatio: null,
        fiveMinuteChange: null,
        sixtyDayChange: null,
        yearChange: null,
        totalMarketCap: null,
        freeMarketCap: null,
        tradeDate: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      debugger
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加股票当前信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getCurrent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改股票当前信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateCurrent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCurrent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除股票当前信息编号为"' + ids + '"的数据项？').then(function() {
        return delCurrent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有股票当前信息数据项？').then(() => {
        this.exportLoading = true;
        return exportCurrent(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    },
    redictDetail(symbol) {
      this.$tab.openPage("股票详情", "/stock/detail");
    }
  }
};
</script>
